package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.Constants;
import com.demo.dto.LoginUserDTO;
import com.demo.entity.Menu;
import com.demo.entity.Role;
import com.demo.entity.User;
import com.demo.exception.ServiceException;
import com.demo.mapper.MenuMapper;
import com.demo.mapper.RoleMapper;
import com.demo.service.UserService;
import com.demo.mapper.UserMapper;
import com.demo.util.JwtUtil;
import com.demo.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-10-10 15:32:13
*/
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    private final MenuMapper menuMapper;
    private final RoleMapper roleMapper;

    @Override
    public UserVo login(LoginUserDTO loginUserDTO) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName, loginUserDTO.getUserName());
        wrapper.eq(User::getPassword, loginUserDTO.getPassword());
        User user = this.getOne(wrapper);
        if (user == null){
            throw new ServiceException(Constants.AJAX_SERVICE_ERROR, "用户名或密码不正确");
        }

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);

        // 生成token
        Map<String, Object> payload = new HashMap<>();
        payload.put("id", user.getId());
        payload.put("userName", user.getUserName());
        String token = JwtUtil.generate(payload);
        userVo.setToken(token);

        // 获取菜单
        String roleFlag = user.getRole();
        List<Menu> menus = menuMapper.selectByRoleFlag(roleFlag);

        // 获取一级
        List<Menu> list = menus.stream()
                .filter(item -> item.getPid() == null)
                .map(item -> {
                    // 获取二级
                    List<Menu> children = menus.stream()
                            .filter(subItem -> item.getId().equals(subItem.getPid()))
                            .collect(Collectors.toList());
                    item.setChildren(children);
                    return item;
                })
                .collect(Collectors.toList());
        userVo.setMenus(list);

        return userVo;
    }

    @Override
    public Page<User> findPage(Integer pageNum, Integer pageSize, User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(user.getUserName()), User::getUserName, user.getUserName());
        wrapper.eq(Objects.nonNull(user.getNickname()), User::getNickname, user.getNickname());
        wrapper.eq(Objects.nonNull(user.getPhone()), User::getPhone, user.getPhone());
        wrapper.eq(Objects.nonNull(user.getRole()), User::getRole, user.getRole());

        // Page<User> page = this.page(new Page<User>(pageNum, pageSize), wrapper);

        // // 查询角色名称
        // page.getRecords().forEach(item -> {
        //     Role role = roleMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getFlag, item.getRole()));
        //     item.setRoleName(role.getName());
        // });

        Page<User> page = this.baseMapper.selectPageList(new Page<>(pageNum, pageSize), wrapper);

        return page;
    }
}




